package action;

import org.apache.struts2.interceptor.validation.SkipValidation;

import seivice.impl.UsersDAOImpl;
import service.UsersDAO;

import com.opensymphony.xwork2.ModelDriven;

import entity.Users;

/**
 * 用户action类
 * @author 请叫我小纯洁丶
 *
 */
public class UsersAction extends SuperAction implements ModelDriven<Users> {
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Users user = new Users();
	
	//用户登录
	public String login(){
		UsersDAO udao = new UsersDAOImpl();
		if(udao.usersLogin(user)){
			
			session.setAttribute("loginUsersName", user.getUsername());
			
			return "login_success";
		}else{
			return "login_failure";
		}
	}
	
	@SkipValidation
	//用户注销
	public String logout(){
		if(session.getAttribute("loginUsersName")!=null){
			session.removeAttribute("loginUsersName");
		}
		return "logout_success";
	}
	
	
	

	@Override
	public void validate() {
		// TODO Auto-generated method stub
		if("".equals(user.getUsername().trim())){
			this.addFieldError("usernameError", "用户名不能为空！");
		}
		if(user.getPassword().length()<6){
			this.addFieldError("passwordError", "密码不能少于6位！");
		}
	}

	@Override
	public Users getModel() {
		// TODO Auto-generated method stub
		return this.user;
	}

}
